package org.groupg.project.a;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//8. 主 POJO 类 - Employee.java
@JacksonXmlRootElement(localName = "EmployeeRecord")
@JsonPropertyOrder({"employeeCode", "fullName", "employmentDate", "salary", "department", "skills", "metadata"})
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Employee {
    @JacksonXmlProperty(isAttribute = true)
    @JsonProperty("empId")
    private String employeeCode;

    // 修复: 使用自定义序列化器处理 FullName
    @JsonSerialize(using = ToStringSerializer.class)
    @JsonDeserialize(converter = StringToFullNameConverter.class)
    @JacksonXmlProperty(localName = "FullName")
    private FullName fullName;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    @JsonSerialize(using = CustomDateSerializer.class)
    @JsonDeserialize(using = CustomDateDeserializer.class)
    private LocalDate employmentDate;

    @JsonIgnore
    private String secretToken;

    private Department department;
    private List<Skill> skills = new ArrayList<>();
    private Map<String, Object> metadata = new HashMap<>();
    private double salary;

    @JsonCreator
    public Employee(
            @JsonProperty("empId") String employeeCode,
            @JsonProperty("monthlySalary") double salary) {
        this.employeeCode = employeeCode;
        this.salary = salary;
    }

    @JsonAnyGetter
    public Map<String, Object> getMetadata() {
        return metadata;
    }

    @JsonAnySetter
    public void addMetadata(String key, Object value) {
        metadata.put(key, value);
    }

    // Getter 和 Setter
    public String getEmployeeCode() {
        return employeeCode;
    }

    public void setEmployeeCode(String employeeCode) {
        this.employeeCode = employeeCode;
    }

    public FullName getFullName() {
        return fullName;
    }

    public void setFullName(FullName fullName) {
        this.fullName = fullName;
    }

    public LocalDate getEmploymentDate() {
        return employmentDate;
    }

    public void setEmploymentDate(LocalDate employmentDate) {
        this.employmentDate = employmentDate;
    }

    public String getSecretToken() {
        return secretToken;
    }

    public void setSecretToken(String secretToken) {
        this.secretToken = secretToken;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public List<Skill> getSkills() {
        return skills;
    }

    public void setSkills(List<Skill> skills) {
        this.skills = skills;
    }

    public void addSkill(Skill skill) {
        skills.add(skill);
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    // 添加 FullName 转换器
    public static class StringToFullNameConverter extends com.fasterxml.jackson.databind.util.StdConverter<String, FullName> {
        @Override
        public FullName convert(String value) {
            return FullName.fromString(value);
        }
    }
}